package com.superzhou.dragview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

/**
 * @author zhoubo
 * @date 2020/8/14
 * @describe
 */
public class DragItemView extends RelativeLayout {

    private View contentView;
    private TextView textNum;
    private ImageView icon;
    private DataBean mData;
    private Animation animation;
    private Animation scaleAnim;
    private DragChangeListener mListener;


    public void setListener(DragChangeListener mListener) {
        this.mListener = mListener;
    }

    public DragItemView(Context context) {
        this(context,null);
    }

    public DragItemView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DragItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void  initIconSource(String sourceName){
        if(icon!=null)
        icon.setImageResource( getResources().getIdentifier(sourceName,"mipmap",getContext().getPackageName()));
    }

    public  void  init(String resource,DragChangeListener mListener){
        this.mListener = mListener;
        contentView = this.findViewById(R.id.content);
        textNum = this.findViewById(R.id.text_number);
        icon = this.findViewById(R.id.image_view);
        icon.setImageResource( getResources().getIdentifier(resource,"mipmap",getContext().getPackageName()));
        animation = AnimationUtils.loadAnimation(contentView.getContext(), R.anim.alpha_anim);
        scaleAnim = AnimationUtils.loadAnimation(icon.getContext(), R.anim.scale_anim);
        this.animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                contentView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                contentView.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
    public DataBean getData() {
        return mData;
    }

    public void setData(DataBean mData) {
        this.mData = mData;
    }

    @SuppressLint("SetTextI18n")
    public void SetNum(int num){
        textNum.setText("+"+num);
    }
    public void StartAnim(){
        this.startAnim = true;
        animHandler.sendEmptyMessageDelayed(1,1000);
    }
    public void StopAnim(){
        this.startAnim = false;
        animation.cancel();
        scaleAnim.cancel();
    }
    public boolean isStartAnim() {
        return startAnim;
    }

    public void setStartAnim(boolean startAnim) {
        this.startAnim = startAnim;
        if (startAnim){
            animHandler.sendEmptyMessageDelayed(1,1000);
        }
    }

    private boolean startAnim = false;
    Handler animHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 1&&startAnim){
                icon.startAnimation(scaleAnim);
                contentView.startAnimation(animation);
                animHandler.sendEmptyMessageDelayed(1,4000);
                if (mListener!=null)
                    mListener.OnChangeNumber(mData.getGoldCount());
            }else if (msg.what == 2){

            }
            return false;
        }
    });
}
